<template>
  <div class="article-lists">
    <el-card class="!border-none" shadow="never">
      <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
        <el-form-item class="w-[280px]" label="关键字">
          <el-input
              v-model="queryParams.code"
              placeholder="输入关键字"
              clearable
              @keyup.enter="resetPage"
          />
        </el-form-item>
        <el-form-item class="w-[280px]" label="状态">
          <el-select v-model="queryParams.status">
            <el-option label="全部" value />
            <el-option label="未使用" :value="0" />
            <el-option label="已使用" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <div>
        <el-button
            @click="generateKeysHandle"
            v-perms="['key.key/add', 'key.key/add:edit']" type="primary" class="mb-4">
          <template #icon>
            <icon name="el-icon-Plus" />
          </template>
          生成密钥
        </el-button>
      </div>
      <el-table size="large" v-loading="pager.loading" :data="pager.lists">
        <el-table-column label="ID" prop="id" min-width="80" />
        <el-table-column
            label="密钥"
            prop="code"
            min-width="160"
            show-tooltip-when-overflow
        />
        <el-table-column label="状态" min-width="100">
          <template #default="{ row }">
            <el-tag v-if="row.status === 0" type="success">未使用</el-tag>
            <el-tag v-if="row.status === 1" type="warning">已使用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="生成时间" prop="create_time" min-width="120" />
        <el-table-column label="激活用户" prop="user_name" min-width="120" />
        <el-table-column label="激活时间" prop="update_time" min-width="100" >
          <template #default="{ row }">
            {{row.update_time ? row.update_time : ''}}
          </template>
        </el-table-column>
<!--        <el-table-column label="操作" width="120" fixed="right">-->
<!--          <template #default="{ row }">-->
<!--            <el-button-->
<!--                v-perms="['key.key/delete']"-->
<!--                type="danger"-->
<!--                link-->
<!--                @click="handleDelete(row.id)"-->
<!--            >-->
<!--              删除-->
<!--            </el-button>-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
  </div>
</template>
<script lang="ts" setup name="keyLists">
import {generateKeys, keyDelete, keyLists, keyStatus} from '@/api/key'
import { useDictOptions } from '@/hooks/useDictOptions'
import { usePaging } from '@/hooks/usePaging'
import { getRoutePath } from '@/router'
import feedback from '@/utils/feedback'

const queryParams = reactive({
  code: '',
  status: '',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
  fetchFun: keyLists,
  params: queryParams
})

const changeStatus = async (is_show: any, id: number) => {
  try {
    await keyStatus({ id, is_show })
    getLists()
  } catch (error) {
    getLists()
  }
}

const generateKeysHandle =  async (is_show: any, id: number) => {
  try {
    await generateKeys()
    getLists()
  } catch (error) {
    getLists()
  }
}

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  await keyDelete({ id })
  getLists()
}

onActivated(() => {
  getLists()
})

getLists()
</script>
